package com.github.tetrisgame.utils;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.MediaPlayer;
import android.media.SoundPool;

import androidx.appcompat.app.AppCompatActivity;

import com.github.tetrisgame.R;
import com.github.tetrisgame.ui.activity.MainActivity;

import java.util.Random;

public class Music {
    public static final Music defaultMusic = new Music();
    private Context context;
    private MediaPlayer mediaPlayer;
    private SoundPool soundPool;
    private int soundMoveId;
    private int soundRotate1Id;
    private int soundRotate2Id;
    private int soundClear1Id;
    private int soundClear2Id;
    private int soundfastDown1Id;
    private int soundfastDown2Id;
    private int soundLevel_upId;
    private int soundGameOverId;
    private Random random = new Random();

    private Music() {
    }
    public void init(Context context){
        this.context = context;
    }
    public void initMp(){
        int num = random.nextInt(3);
        int[] bgms = new int[]{R.raw.bgm1,R.raw.bgm2,R.raw.bgm3};
        mediaPlayer = MediaPlayer.create(context, bgms[num]);
        mediaPlayer.setLooping(true);
        mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build());
        mediaPlayer.start();
    }
    public void clearMp(){
        if (mediaPlayer != null) {
            mediaPlayer.setOnCompletionListener(mp -> {

            });
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
    public void initSp(){
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        soundPool = new SoundPool.Builder()
                .setMaxStreams(5)
                .setAudioAttributes(audioAttributes)
                .build();
        soundMoveId = soundPool.load(context,R.raw.move,1);
        soundRotate1Id = soundPool.load(context,R.raw.rotate1,1);
        soundRotate2Id = soundPool.load(context,R.raw.rotate2,1);
        soundClear1Id = soundPool.load(context,R.raw.clear1,1);
        soundClear2Id = soundPool.load(context,R.raw.clear2,1);
        soundfastDown1Id = soundPool.load(context,R.raw.fast_down1,1);
        soundfastDown2Id = soundPool.load(context,R.raw.fast_down2,1);
        soundLevel_upId = soundPool.load(context,R.raw.level_up,1);
        soundGameOverId = soundPool.load(context,R.raw.game_over,1);
    }
    public void moveMusic(){
        soundPool.play(soundMoveId,1.0f,1.0f,0,0,1.0f);
    }
    public void rotateMusic(){
        int num = random.nextInt(2);
        soundPool.play(num == 0 ? soundRotate1Id : soundRotate2Id,1.0f,1.0f,0,0,1.0f);
    }
    public void clearMusic(){
        int num = random.nextInt(2);
        soundPool.play(num == 0 ? soundClear1Id : soundClear2Id,1.0f,1.0f,0,0,1.0f);
    }
    public void fast_downMusic(){
        int num = random.nextInt(2);
        soundPool.play(num == 0 ? soundfastDown1Id : soundfastDown2Id,1.0f,1.0f,0,0,1.0f);
    }
    public void level_upMusic(){
        soundPool.play(soundLevel_upId,1.0f,1.0f,0,0,1.0f);
    }
    public void GameOverMusic(){
        soundPool.play(soundGameOverId,1.0f,1.0f,0,0,1.0f);
    }

    public void clearSp(){
        soundPool.release();
    }

}
